<template>
    <base-breadcrumb :title="$route.meta.title">
        <!-- 党务监督编辑 -->
        <a-form-model
            ref="ruleForm"
            :model="form"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 18 }"
            :rules="editModel != 0 ? rules : {}"
        >
            <a-spin :spinning="loading">
                <!-- 表格 -->
                <a-card :bordered="false">
                    <title-name title="基本信息"></title-name>
                    <custom-form
                        :formList="formList"
                        :form="form"
                        @onSelectChange="onSelectChange"
                        :editModel="editModel == 0 ? '' : ''"
                    >
                    </custom-form>
                </a-card>
                <a-card :bordered="false">
                    <!-- 附件上传 -->
                    <title-name title="完成上报"></title-name>
                    <a-row :gutter="24">
                        <a-col :md="8" :sm="8" v-if="editModel != 0">
                            <a-form-model-item label="上报单位">
                                <BaseInput disabled placeholder="请输入" v-model="form.orgName" />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="24" :sm="24" v-if="editModel != 0">
                            <a-form-model-item label="*整改材料上传" prop="fileInfos">
                                <l-table count="5" businessCode="OF05" :uploadArrys.sync="form.fileInfos"></l-table>
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="24" :sm="24" v-else>
                            <a-form-model-item label="*整改材料上传">
                                <file-link :fileList="form.fileInfos"></file-link> </a-form-model-item
                        ></a-col>

                        <a-col :md="24" :sm="24">
                            <div class="file_prompt ml20">
                                支持格式：.rar .zip doc. docx. xls. pdf. jpg .png，单个附件大小不得超过200M
                            </div>
                        </a-col>
                    </a-row>
                    <!-- </a-form-model> -->
                </a-card>
            </a-spin>
            <!-- 页脚操作栏 -->
            <div class="btn_box page-btn-right-top">
                <a-button
                    :loading="loading"
                    v-if="editModel != 0"
                    type="primary"
                    class="button_right"
                    @click="handleSubmit"
                    >提交</a-button
                >
                <a-button type="primary" @click="handleActiveClose">关闭</a-button>
            </div>
        </a-form-model>
    </base-breadcrumb>
</template>

<script>
import {
    getSuperviseTaskUp,
    updateSuperviseTaskUp,
    
} from '@/api/disciplineInspection/supervision' // api
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import Selector from '@/components/Select/Selector'

import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject'
import SelectOrgPeople from '@/components/Select/SelectOrgPeople'

export default {
    name:'a'+ Date.now(),
    extends: new BaseEditPage(),
    components: {
        SelectSubCompany,
        SelectOrgPeople,
    },
    data() {
        return {
            customRender: (q) => {
                let result = q.name
                if (q.postPNames && q.postPNames.length) {
                    result = `${result} (${q.postPNames.join('/')}) `
                }
                return result
            },
            loading: false, // 加载控件
            editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
            form: { personIds: [] }, // 表单
            rules: {
                fileInfos: [{ type: 'array', required: true, message: '请上传附件', trigger: 'blur' }],
            }, // 校验规则
            init: false, // 初始化控件
        }
    },
    watch: {
        // 监听附件实现附件校验
        'form.fileInfos'() {
            if (this.editModel != 0 && this.init) {
                this.$nextTick(() => {
                    this.$refs.ruleForm?.validateField('fileInfos')
                })
            }
        },
    },
    mounted() {
        this.$route.meta.title = ['监督任务上报查看', '监督任务上报编辑', '监督任务上报新增'][this.editModel]
        // 不为添加时请求详情
        if (this.editModel != 2) {
            this.loading = true
            getSuperviseTaskUp(this.id)
                .then((res) => {
                    this.loading = false
                    this.form = res.data
                    res.data.date = []
                    res.data.date[0] = res.data.taskDateStart
                    res.data.date[1] = res.data.taskDateEnd
                    this.$nextTick(() => {
                        this.init = true
                    })
                })
                .catch(() => {
                    this.loading = false
                })
        } else {
            this.$nextTick(() => {
                this.init = true
            })
        }
    },
    computed: {
        formList() {
            return [
                {
                    keyName: 'levelStr',
                    type: 'BaseInput',
                    label: '所属层级',
                    disabled: true,
                },
                {
                    keyName: 'taskName',
                    type: 'BaseInput',
                    label: '任务名称',
                    disabled: true,
                },
                {
                    keyName: 'date',
                    type: 'a-range-picker',
                    placeholder: ['请选择日期', '请选择日期'],
                    disabled: true,
                    label: '时间',
                    attrs: {
                        valueFormat: 'YYYY-MM-DD',
                    },
                },
                {
                    keyName: 'content',
                    type: 'BaseTextarea',
                    label: '主要内容',
                    disabled: true,
                    span: 24,
                },
            ]
        },
    },
    methods: {
        onSelectChange(name, e) {
            if (e?.key == 'adminId') {
                this.form.phone = e.phone
            }
            if (e?.key == 'chargeId') {
                this.form.chargePhone = e.phone
                this.form.jobPost = e.postPNames.toString()
                console.log(this.form)
            }
        },
        /**
         * 提交表单
         */
        handleSubmit() {
            let that = this
            // let api = [updateUnionBookHouse, addUnionBookHouse][that.editModel - 1] // 根据操作切换url地址
            // 校验表单
            that.$refs.ruleForm.validate((e) => {
                if (e) {
                    // 提交表单
                    that.loading = true
                    let form = that.$clone(that.form)

                    updateSuperviseTaskUp(form)
                        .then(() => {
                            that.loading = false
                            that.$message.success('提交成功！')
                            that.$multiTab.close()
                        })
                        .catch(() => {
                            that.loading = false
                        })
                }
            })
        },
    },
}
</script>

<style lang="less" scoped>
.file_prompt {
    color: #999;
}
</style>